package com.carkeeper.carType.service.impl;

import com.carkeeper.carType.mapper.CarTypeMapper;
import com.carkeeper.carType.service.CarTypeService;
import com.carkeeper.common.utils.Result;
import com.carkeeper.common.utils.StatusCode;
import com.carkeeper.pojo.Brand;
import com.carkeeper.pojo.BrandType;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * @author zhangmingxin
 * @version 1.0
 * @date 2020/11/24 20:25
 */
@Service
public class CarTypeServiceImpl implements CarTypeService {

    @Autowired
    private CarTypeMapper carTypeMapper;

    /**
     * 新增车标
     * @param brand
     */
    @Override
    public void add(Brand brand) {
        carTypeMapper.addCar(brand);
        this.findAll();
    }

    /**
     * 批量新增poi表数据
     * @param brandList
     */
    @Override
    public void addBrand(List<Brand> brandList) {
        for (Brand brand : brandList) {
            carTypeMapper.insert(brand);
            this.findAll();
        }
    }

    /**
     * 品牌列表查询
     * @return
     */
    @Override
    public List<Brand> findAll() {
        return carTypeMapper.selectAll();
    }

    /**
     * 条件查询(车标名称+国别)
     * @param brand
     * @return
     */
    @Override
    public Result findByExample(Brand brand) {
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        //根据条件进行查询
        if (brand.getName()!=null&&!"".equals(brand.getName())) {
            criteria.andEqualTo("name", brand.getName());
        }
        if (brand.getCountry()!=null&&!"".equals(brand.getCountry())){
            criteria.andEqualTo("country",brand.getCountry());
        }
        //根据条件查询
        List<Brand> brandList = carTypeMapper.selectByExample(example);
        return new Result(true, StatusCode.OK,"查询成功",brandList);
    }

    /**
     *根据品牌id查询品牌类别
     * @param id
     * @return
     */
    @Override
    public List<BrandType> findById(Integer id) {
       //根据品牌表的id查询品牌类别下的分类信息
        return carTypeMapper.selectById(id);
    }

    /**
     * 条件+分页查询
     * @param searchMap
     * @param pageNum 当前页
     * @param pageSize 每页显示条数
     * @return
     */
    @Override
    public Page<Brand> findPage(Map searchMap, Integer pageNum, Integer pageSize) {
        //1.查询
        PageHelper.startPage(pageNum,pageSize);
        Example example=new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            //根据条件进行查询
            if (searchMap.get("name")!=null&&!"".equals(searchMap.get("name"))) {
                criteria.andEqualTo("name", searchMap.get("name"));
            }
            if (searchMap.get("country")!=null&&!"".equals(searchMap.get("country"))){
                criteria.andEqualTo("country",searchMap.get("country"));
            }
        }
        Page<Brand> brandList = (Page<Brand>) carTypeMapper.selectByExample(example);
        //根据条件查询
        return  brandList;

    }

}
